package com.zhisheng.springbootredis.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.zhisheng.springbootredis.entity.Student;
import com.zhisheng.springbootredis.mapper.StudentMapper;
import com.zhisheng.springbootredis.service.StudentService;
import com.zhisheng.springbootredis.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.Map;

/**
 * (Student)表服务实现类
 *
 * @author 杨小龙
 * @since 2025-10-20 18:59:03
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public R insert(Student student) {
        //使用redis存储数据
        HashOperations forHash = redisTemplate.opsForHash();

        //将对象转换成字符串
        String s = JSONObject.toJSONString(student);
        HashMap<String, String> h1 = new HashMap<>();
        h1.put("学生",s);
        forHash.putAll("s:"+student.getId(),h1);
        System.out.println(forHash.entries("s:"+student.getId()));
        return R.success("添加"+student+"成功了");
    }
    public R selectById(Integer id){
        HashOperations forHash = redisTemplate.opsForHash();
        Map entries = forHash.entries("s:" + id);
        return R.success(entries);
    }


}
